package org.holynite.fieldmanage.managed.support

import org.holynite.fieldmanage.ejbs.AdministratorManagement
import org.holynite.fieldmanage.entities.Administrator

import javax.ejb.EJB
import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.faces.context.FacesContext
import javax.servlet.http.HttpSession

/**
 * Created by midnite on 15/1/31.
 */
@ManagedBean
@SessionScoped
class LoginSessionBean implements Serializable {
    Administrator loginAdmin
    HttpSession session

    @EJB(beanName = "adminControl")
    AdministratorManagement adminMan

    LoginSessionBean() {
        session = FacesContext.currentInstance.externalContext.getSession(true) as HttpSession
    }

    Boolean checkPrivilege(String function) {
        if (adminMan.isDefaultAdministrator(loginAdmin)) return true
        loginAdmin.role.any { it.privileges.any { it.privilege.codeSign.equals(function) } }
    }

    String lockDown() {
        session.setAttribute("lockdown", true)
        return "lockdown"
    }

    String logout() {
        session.setAttribute("loggedin", false)
        loginAdmin = null
        return "login"
    }
}
